﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Way.Bus.Controllers;
using Way.Bus.WMS.Model;
using Way.Contract.Data;
using Way.EAP.DataAccess.Regulation;
using Way.Bus.Wms.Controllers.BaseData;
using Way.Contract.Data.ControllerData;
using Way.Contract.MVC;
using Way.EAP.DataAccess.Data;
using System.Data;
using Way.EAP.DataAccess.Entity;
using Way.Bus.Model;

namespace Way.Bus.WMS.ST.Controllers
{
    public class StockInfoController : BusinessController
    {
        public override bool IsClearAway
        {
            get
            {
                return false;
            }
        }
        public override bool IsPrint
        {
            get
            {
                return false;
            }
        }
        protected override void OnInitViewChildItem(ModelData data, ItemData item)
        {
            //base.OnInitViewChildItem(data, item);
            if (item.IsField("rowindex"))
                item.visible = false;
            if (data.name.EndsWith("StockInfoView"))
            {
                if (item.IsField("SUBNO"))
                    item.title = "货物有效期";
                //隐藏属性信息
                if (item.IsField("KNAME")
                      || item.IsField("LNAME")
                      || item.IsField("MCODE")
                      || item.IsField("MNAME")
                      || item.IsField("MODEL")
                      || item.IsField("WNAME")
                      || item.IsField("WAREHOUSENAME")
                      //|| item.IsField("DCODE")
                      //|| item.IsField("BATCHNO")
                      //|| item.IsField("SUBNO")
                      || item.IsField("KYNUM")
                      || item.IsField("NUM")
                      || item.IsField("ZYNUM")
                      || item.IsField("LASTINTIME")
                    )
                    item.disabled = true;
            }
            if (data.name.EndsWith("InfoInSequence"))
            {
                if (item.IsField("SEQUENCECODE"))
                    item.visible = true;
                else
                    item.visible = false;
                if (item.IsField("STOCKINFOMATERIALSID"))
                {
                    if (item.foreign != null)
                    {
                        item.foreign.foreignobject = typeof(StockInfoView).FullName;
                        item.foreign.foreignfiled = "ID";
                    }
                }

            }
        }
        public StockInfoController() : base(new StockInfoView()) { }

      
        protected override ActionCommand[] OnInitCommand(ActionCommand[] commands)
        {
            ActionCommand[] cmd = base.OnInitCommand(commands);
            //获取所有按钮集合
            foreach (ActionCommand ac in cmd)
            {
                if (ac.command == "add")
                {
                    ac.visible = false;
                }
                if (ac.command == "edit")
                {
                    ac.name = "修改序列号";
                }
                if (ac.command == "remove")
                {
                    ac.visible = false;
                }
                if (ac.command == "SubmitData")
                {
                    ac.visible = false;
                }
                if (ac.command == "OnSubmitData")
                {
                    ac.visible = false;
                }
                if (ac.command == "ReviewedData")
                {
                    ac.visible = false;
                }
                if (ac.command == "ImportExcel")
                {
                    ac.visible = false;
                }
            }
            return cmd;
        }

        [ActionCommand(name = "导出", title = "导出", index = 10, icon = "icon-down", isselectrow = false, onclick = "emport1")]
        public void Emport()
        {
            //ExcelDRController er = new ExcelDRController();
            //er.bc = this;
        }

        protected override void OnEditing(ControllerBase.SaveEvent item)
        {
            StockInfoView view = item.Item as StockInfoView;
            if (view != null)
            {
                string sql = "update Acc_WMS_InSequence set SEQUENCECODE ='{0}' where SEQUENCECODE='{1}'";
                string sql1 = "update Acc_WMS_InfoMaterials set BATCHNO='{1}',SENBATCHNO='{2}',DEPOTWBS='{3}' where id={0}";
                IDataAction action = this.model.GetDataAction();
                action.StartTransation();
                try
                {
                    int did = WareHouse.GetDepotwesID(view.WID, view.DCODE, action);
                    action.Execute(string.Format(sql1, view.ID, view.BATCHNO, view.SUBNO, did));
                    foreach (InfoInSequence seq in view.StockSequence)
                    {
                        IEntityBase ieb = seq;
                        InfoInSequence oldseq = ieb.GetOldObject as InfoInSequence;
                        if (!string.IsNullOrEmpty(oldseq.SEQUENCECODE) && oldseq.SEQUENCECODE != seq.SEQUENCECODE)
                        {
                            action.Execute(string.Format(sql, seq.SEQUENCECODE, oldseq.SEQUENCECODE));
                        }
                    }
                    action.Commit();
                    action.EndTransation();
                }
                catch (Exception e)
                {
                    action.EndTransation();
                    throw e;
                }
            }

            //base.OnEditing(item);
        }

        protected override void OnEdited(EntityBase item)
        {
            base.OnEdited(item);
           // ChangeSequenceCode(item);//谢请参考Editing中的代码
        }

        //修改入库单中该商品对应的序列码
        public void ChangeSequenceCode(EntityBase item)
        {
            EntityList<StockInfoView> list = ((IEntityBase)item).ParentList as EntityList<StockInfoView>;

            if (list != null && list.Count>0)
            {
                string[] o_seqlist = new string[list[0].StockSequence.Count];
                for (int i = 0; i < list[0].StockSequence.Count; i++)
                {
                    o_seqlist[i] = list[0].StockSequence[i].SEQUENCECODE;
                }
                
                StockInfoView stockinfo = item as StockInfoView;
                IDataAction action = this.model.GetDataAction();
                try
                {
                    action.StartTransation();
                    for (int i = 0; i < stockinfo.StockSequence.Count; i++)
                    {
                        string o_sequencecode = o_seqlist[i];
                        string n_sequencecode = stockinfo.StockSequence[i].SEQUENCECODE;
                        string sql = "update Acc_WMS_InSequence " +
                            "set SEQUENCECODE = '" + n_sequencecode + "' " +
                            "where InOrderMATERIALID in ( " +
                            "select ID from Acc_WMS_InOrderMaterials " +
                            "where MCODE='" + stockinfo.MCODE + "' " +
                            ") " +
                            "and SEQUENCECODE = '" + o_sequencecode + "'";
                        action.Execute(sql);
                    }
                    action.Commit();
                }
                catch (Exception err)
                {   
                    throw err;
                }
                finally
                {
                    action.EndTransation();
                }
            }

        }

        protected override void OnGetWhereing(IModel m, List<SQLWhere> where, loadItem item)
        {
            base.OnGetWhereing(m, where, item);
            WmsRoleDataController.SearchManage(this, where, item);
        }
        [WhereParameter]
        public string sn { get; set; }

        public ReadTable GetSnTable()
        {
            InfoInSequence iis = new InfoInSequence();
            string sql = "select STOCKINFOMATERIALSID from " + iis.ToString() + " where SEQUENCECODE='{0}'";
            if (!string.IsNullOrEmpty(sn))
            {
                IDataAction action = this.model.GetDataAction();
                object obj = action.GetValue(string.Format(sql, sn));
                if (obj != null && !string.IsNullOrEmpty(obj.ToString()))
                {
                    if (this.LoadItem == null)
                        this.LoadItem = new loadItem();

                    this.LoadItem.page = "1";
                    if (string.IsNullOrEmpty(this.LoadItem.rows))
                        this.LoadItem.rows = "10";
                    List<SQLWhere> list = new List<SQLWhere>();
                    SQLWhere w = new SQLWhere();
                    w.ColumnName = "ID";
                    w.Value = obj.ToString();
                    list.Add(w);
                    this.LoadItem.whereList = list.ToArray();
                    return this.SearchData(this.LoadItem);
                }
                else
                {
                    throw new Exception("查询的系列码" + sn + "在库存中未找到！");
                }
            }
            else
            {
                throw new Exception("查询的系列码号不能为空！");
            }
        }
    }
}
